package com.younion.business.impl;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.business.IActiveService;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.dao.ActiveDAO;
import com.younion.vo.ActiveVO;

import cpcns.jhsks.util.MapCollectionUtils;

public class ActiveServiceImpl extends BaseService implements IActiveService {

	protected static final Log logger = LogFactory
			.getLog(ActiveServiceImpl.class);

	private ActiveDAO activeDAO;

	public boolean deleteActiveById(int id) throws BaseException {
		try {
			return this.activeDAO.deleteActiveById(id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public boolean deleteActiveById(int id) error occur:",
					e);
		}
	}

	public boolean deleteActiveByIds(List list) throws BaseException {
		try {
			return this.activeDAO.deleteActiveByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public boolean deleteActiveByIds(List list) error occur:",
					e);
		}
	}

	public Integer insertByVo(ActiveVO activeVO) throws BaseException {
		try {
			return this.activeDAO.insertByVo(activeVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public Integer insertByVo(ActiveVO activeVO) error occur:",
					e);
		}
	}

	public ActiveVO selectActiveById(int id) throws BaseException {
		try {
			return this.activeDAO.selectActiveById(id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public ActiveVO selectActiveById(int id) error occur:",
					e);
		}
	}

	public List<ActiveVO> selectByIds(List list) throws BaseException {
		try {
			return this.activeDAO.selectByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public List<ActiveVO> selectByIds(List list) error occur:",
					e);
		}
	}

	public int selectCountByMap(Map paramMap) throws BaseException {
		try {
			return this.activeDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public int selectCountByMap(Map paramMap) error occur:",
					e);
		}
	}

	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (MapCollectionUtils.isEmpty(condition))
			return null;
		try {
			Map map = new HashMap();
			map.put("paramMap", condition);
			return this.activeDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public List selectForPage(Map condition, Paginator paginator) error occur:",
					e);
		}
	}

	public int selectMaxTop() throws BaseException {
		try {
			return this.activeDAO.selectMaxTop();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(ActiveServiceImpl.class.getName()
					+ " >> public int selectMaxTop() error occur:", e);
		}
	}

	public boolean updateActiveByMap(Map paramMap) throws BaseException {
		try {
			return this.activeDAO.updateActiveByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public boolean updateActiveByMap(Map paramMap) error occur:",
					e);
		}
	}

	public void updateTopById(Map map) throws BaseException {
		try {
			this.activeDAO.updateTopById(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(ActiveServiceImpl.class.getName()
					+ " >> public void updateTopById(Map map) error occur:", e);
		}
	}

	public List<ActiveVO> selectActivitysByCardMap(Map map)
			throws BaseException {
		try {
			return this.activeDAO.selectActivitysByCardMap(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					ActiveServiceImpl.class.getName()
							+ " >> public void selectActivitysByCardMap(Map map) error occur:",
					e);
		}
	}

	public Collection getApiAll(Map map) throws Exception {
		return activeDAO.selectApiAll(map);
	}

	public ActiveVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id) < 1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = activeDAO.selectActiveById(Integer.parseInt(id));
			return obj == null ? null : (ActiveVO) obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardCountInitServiceImpl.class.getName()
					+ " >> public cardcountinitVO get(String id) error occur:",
					e);
		}
	}
	public boolean deleteImgByCustomerIds(List<String> customerIds , String path) throws BaseException{
		if(CollectionUtils.isEmpty(customerIds)){
			return false;
		}
		try{
			Map map = new HashMap();
			for(String customerId : customerIds){
				map.put("customerId", customerId);
				List<ActiveVO> activeList = (List<ActiveVO>)activeDAO.selectByMap(map);
				for(ActiveVO active : activeList){
					File directory = new File(path + File.separator
							+ active.getId());
					String fileName = active.getPicName();
					ImageFileUtil.deleteImage(directory, fileName, true);	
				}
				map.clear();
			}
			return true;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(CardCountInitServiceImpl.class.getName()+" >> public boolean deleteImgByCustomerIds(List customerIds , String path) error occur:",e);
			
		}
	}

	public ActiveDAO getActiveDAO() {
		return activeDAO;
	}

	public void setActiveDAO(ActiveDAO activeDAO) {
		this.activeDAO = activeDAO;
	}

}